﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    public class TaiKhoanDA
    {
        public TaiKhoanDA()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(TaiKhoanEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("UserName", entity.UserName);
                pb.AddParameter("Password", entity.Password);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("IDGU", entity.IDGU);
                return (int)DBFactory.Database.ExecuteNonQuery("TaiKhoan_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(TaiKhoanEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDTK", entity.IDTK);
                pb.AddParameter("UserName", entity.UserName);
                pb.AddParameter("Password", entity.Password);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("IDGU", entity.IDGU);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("TaiKhoan_UpDate", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="oldpass"></param>
        /// <param name="newpass"></param>
        /// <returns></returns>
        public bool ChangePass(string username, string oldpass, string newpass)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("UserName", username);
                pb.AddParameter("OldPass", oldpass);
                pb.AddParameter("NewPass", newpass);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("TaiKhoan_ChangePass", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

       
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable Find_All()
        {
            try
            {
                return DBFactory.Database.FillDataTable("SELECT * FROM TaiKhoan");
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public DataTable Find_UserName(string UserName)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("UserName", UserName);

                dt = DBFactory.Database.FillDataTable("TaiKhoan_Find_UserName", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaNV"></param>
        /// <returns></returns>
        public DataTable Find_MaNV(int MaNV)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaNV", MaNV);

                dt = DBFactory.Database.FillDataTable("TaiKhoan_Find_MaNV", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            try
            {
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDTK", id);

                return DBFactory.Database.ExecuteNonQuery("TaiKhoan_Delete", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public bool Delete_UserName(string UserName)
        {
            try
            {
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("UserName", UserName);

                return DBFactory.Database.ExecuteNonQuery("TaiKhoan_Delete_UserName", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<TaiKhoanEntity> ConvertToList(DataTable dt)
        {
            List<TaiKhoanEntity> list = new List<TaiKhoanEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int idtk = (int)dt.Rows[i][0];
                string username = dt.Rows[i][1].ToString();
                string password = dt.Rows[i][2].ToString();
                int MaNV = (int)dt.Rows[i][3];
                int IDGU = (int)dt.Rows[i][4];

                TaiKhoanEntity a = new TaiKhoanEntity(idtk, username, password, MaNV, IDGU);
                list.Add(a);
            }
            return list;
        }

    }
}
